module.exports = {
	now(){
		var date = new Date(),
			year = date.getFullYear(), //年
			month = date.getMonth(), //月
			day = date.getDate(), //日
			hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
			minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(), //分
			second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
		month >= 1 && month <= 9 ? (month = "0" + month) : "";
		day >= 0 && day <= 9 ? (day = "0" + day) : "";
		hour >= 0 && hour <= 9 ? hour : "";
		minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
		second >= 0 && second <= 9 ? (second = "0" + second) : "";
		let str  = 'yyyy-m-d h:i:s';
		str = str.replace('yyyy', year).replace('m', month).replace('d', day).replace('h', hour).replace('i', minute).replace('s', second);
		return {date:str,year:year,month:month,day:day,hour:hour,minute:minute,second:second};
	},
	timestamp(time=''){
		var date;
		if (!time){
			date = new Date();
		}else if (isNaN(time)){
			date = new Date(time);
		}else if (String(time).length==10){
			return parseInt(time)
		}else if (String(time).length==13){
			return Math.round(parseInt(time)/1000)
		}else{
			return Math.round(Date.parse(time)/1000)
		}

		return Math.round(date.getTime()/1000);
	},
	
	countdown(timestamp, formats,timeout=''){
	
		formats = formats || '{d}天{h}时{m}分';
		const default_formats = {'minute':'{d}天{h}时{m}分{s}秒','second':'{d}天{h}时{m}分','hour':'{d}天{h}时','H':'{h}时'}
		if (formats in default_formats){
			formats = default_formats[formats];
		}
		if(timestamp > 0){
			let day = Math.floor(timestamp / (60 * 60 * 24));
			let hour = Math.floor(timestamp / (60 * 60)) - (day * 24);
			let minute = Math.floor(timestamp / 60) - (day * 24 * 60) - (hour * 60);
			let second = Math.floor(timestamp) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
		
			if (formats.indexOf('{d}')<0 && day>0){
				hour = hour+day*24;
			}
			//if (day <= 9) day = '0' + day;
			//if (hour <= 9) hour = '0' + hour;
			//if (minute <= 9) minute = '0' + minute;
			//if (second <= 9) second = '0' + second;
			return formats.replace(/\{m\}|\{d\}|\{h\}|\{s\}/ig, function(matches) {
				return ({
					'{d}': day,
					'{h}': hour,
					'{m}': minute,
					'{s}': second
				})[matches];
			});
		}else{
			return timeout;
		}
	},
	daycount(timestamp){
		var date = new Date();
		return Math.round((date.getTime()/1000-timestamp)/(24*3600));
	},
	format(timestamp, formats){ //时间戳转年月日
		
		formats = formats || 'Y-m-d';
		let default_formats = {'date':'Y-m-d','time':'Y-m-d H:i:s','second':'Y-m-d H:i','short_second':'m-d H:i','hour':'Y-m-d H点'}
		// !!!毫秒格式
		// formats格式包括
		// 1. Y-m-d
		// 2. Y-m-d H:i:s
		// 3. Y年m月d日
		// 4. Y年m月d日 H时i分
		if (formats in default_formats){
			formats = default_formats[formats];
		}
	
		var zero = function(value) {
			if (value < 10) {
				return '0' + value;
			}
			return value;
		};
		if (!timestamp || timestamp=='now'){
			timestamp = 0;
		}else if (isNaN(timestamp) ){
			return timestamp;
		}
		var myDate = timestamp ? new Date(String(timestamp).length==10?timestamp*1000:timestamp) : new Date();
	
		var year = myDate.getFullYear();
		var month = zero(myDate.getMonth() + 1);
		var day = zero(myDate.getDate());
	
		var hour = zero(myDate.getHours());
		var minite = zero(myDate.getMinutes());
		var second = zero(myDate.getSeconds());
	
	
		return formats.replace(/Y|m|d|H|i|s/ig, function(matches) {
			return ({
				Y: year,
				m: month,
				d: day,
				H: hour,
				i: minite,
				s: second
			})[matches];
		});
	}
 
}
